const express = require("express");
const {finduserbyid} = require("../../module/handleSeller");
const {findgoods} = require("../../module/handlegoods");

const router = new express.Router();

//卖家注册
router.get("/register",(request,response)=>{
    response.render("seller/register")
})

//买家注册
router.get("/login",(request,response)=>{
    response.render("seller/login")
})

//以下其他页面需登陆才能访问
//判断没有cookie，无登录，重定向到login
router.use((request,response,next)=>{
    if(typeof(response.cookie.get("sellerID")) == "undefined"){
        response.redirect("/login");
    }else{
        next();
    }
})

//商品管理
router.get("/goodslist",(request,response)=>{
    findgoods(response.cookie.get("sellerID"))
    .then(
        (result)=>{
            response.render("seller/goodslist",{goodslist:"active",goods:result})
        },
        (msg)=>{
            response.json({
                message: msg,
                message2: "商家没有上传过商品"
            })
        }
    )
})

//新增商品
router.get("/add-goods",(request,response)=>{
    response.render("seller/addgoods",{goodslist:"active"})
})

//订单管理
router.get("/order-list",(request,response)=>{
    response.render("seller/order-list",{orderlist:"active"})
})

//卖家中心
router.get("/seller",(request,response)=>{
    let id = response.cookie.get("sellerID");
    finduserbyid(id)
    .then(
        (result)=>{
            response.render("seller/seller",{result})
        },
        (msg)=>{
            response.json({
                message:msg
            })
        }
    )
    
})

//退出登录
router.get("/logout",(request,response)=>{
    response.cookie.set("sellerID",null);
    response.redirect("/login");
})


module.exports = router;